﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.Sql;
using System.Data.SqlClient;
using System.Globalization;
using System.IO;

namespace BT_web__nhom06
{
    public partial class Them_ThongtinHD : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void btnsua_Click(object sender, EventArgs e)
        {
            if (IsValid == false) //neu 1 trong cac validator kiem tra khong hop le thi IsValid ==false
            {
                return;
            }
                string mahoatdong = txt_mahoatdong.Text;
                if (mahoatdong != null && mahoatdong.Length <=6)
                {
                    
                    string tenhoatdong = txttenhoatdong.Text;
                    string mota = txtmota.Text;
                    DateTime tgbatdau = DateTime.Parse(txtthoigianbatdau.Text, new CultureInfo("fr-FR"));
                    DateTime tgketthuc = DateTime.Parse(txtthoigianketthuc.Text, new CultureInfo("fr-FR"));
                    string diadiem = txtdiadiem.Text;
                    string hotennguoiphutrach = txthotennguoiphutrach.Text;
                    string sodtnguoiphutrach = txtdienthoai.Text;
                    string tinhtrang = dlsttinhtrang.SelectedValue.ToString();
                    int ketqua = 0;
                    string lenhsql = "insert into [hoatdongdoan]([mahoatdong],[tenhoatdong],[mota],[tgbatdau],[tgketthuc],[diadiem],[hotennguoiphutrach],[sodtnguoiphutrach],[tinhtrang]) values(@mahoatdong, @tenhoatdong ,@mota,@tgbatdau, @tgketthuc, @diadiem, @hotennguoiphutrach, @sodtnguoiphutrach, @tinhtrang)";
                    ketqua = Tienichsql.ketnoicsdl.chaylenhcapnhatdulieu(lenhsql, new string[] { "@mahoatdong", "@tenhoatdong", "@mota", "@tgbatdau", "@tgketthuc", "@diadiem", "@hotennguoiphutrach", "@sodtnguoiphutrach", "@tinhtrang" }, new object[] { mahoatdong, tenhoatdong,mota,tgbatdau,tgketthuc,diadiem,hotennguoiphutrach,sodtnguoiphutrach,tinhtrang });
                    if (ketqua == 1)
                    {
                        Label1.Text = "Thêm thành công có thể tiếp tục thêm một hoạt động mới";
                        txt_mahoatdong.Text = "";
                        txttenhoatdong.Text = "";
                        txtmota.Text = "";
                        txtthoigianbatdau.Text = "";
                        txtthoigianketthuc.Text = "";
                        txtdiadiem.Text = "";
                        txthotennguoiphutrach.Text = "";
                        txtdienthoai.Text = "";    
                    }
                    else
                        Label1.Text = "Thêm không thành công";
                }
               
        }

        protected void CustomValidator1_ServerValidate(object source, ServerValidateEventArgs args)
        {
            string mahoatdong = txt_mahoatdong.Text;
            string sqlSelect = "SELECT * FROM [hoatdongdoan] WHERE [mahoatdong]=@mahoatdong";
            DataTable tblKetQua = Tienichsql.ketnoicsdl.ChayLenhSelectDuLieu(sqlSelect, "@mahoatdong", mahoatdong);
            if (tblKetQua.Rows.Count > 0)
                args.IsValid = false;
            else
                args.IsValid = true;
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            Response.Redirect("QL_hoatdongdoan.aspx");
        }
    }
}